package com.tosit.bmx.common.util;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author tsl
 * @date 2018/4/16 18:23
 */
public class TibetanUtil {
    //请求地址
    public static final String REQUEST_SITE = "http://mt.utibet.edu.cn/mt";

    /**
     * 中文翻译成藏文
     * @author tsl
     * @date 2018/4/16 18:23
     * @param translateText 中文字符串
     * @return
     */
    public static String translateToTibetan(String translateText) {
        String tibetanText = null;
        // 创建默认的httpClient实例.
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 创建httppost
        HttpPost httppost = new HttpPost(REQUEST_SITE);
        // 创建参数队列
        List<NameValuePair> formparams = getNameValuePairs(translateText);
        tibetanText = connectTranslate(tibetanText, httpclient, httppost, formparams);
        return tibetanText;
    }

    /**
     * httpclient连接到翻译网页，获取藏文
     *
     * @author tsl
     * @date 2018/4/16 18:23
     * @param tibetanText
     * @param httpclient
     * @param httppost
     * @param formparams
     * @return
     */
    private static String connectTranslate(String tibetanText, CloseableHttpClient httpclient, HttpPost httppost, List<NameValuePair> formparams) {
        try {
            UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
            httppost.setEntity(uefEntity);
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    //       html解析
                    tibetanText = htmlParse(tibetanText, entity);
                }
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return tibetanText;
    }

    /**
     * html解析
     * @author tsl
     * @date 2018/4/16 18:23
     * @param tibetanText     * @param entity
     * @return
     * @throws IOException
     */
    private static String htmlParse(String tibetanText, HttpEntity entity) throws IOException {
        String s = EntityUtils.toString(entity, "UTF-8");
        Document parse = Jsoup.parse(s);
        Element content = parse.getElementById("txt2");
        tibetanText = content.text();
        return tibetanText;
    }

    /**
     * 创建参数队列
     * @author tsl
     * @date 2018/4/16 18:23
     * @param translateText
     * @return
     */
    private static List<NameValuePair> getNameValuePairs(String translateText) {
        List<NameValuePair> formparams = new ArrayList<>();
        formparams.add(new BasicNameValuePair("src", translateText));
        formparams.add(new BasicNameValuePair("lang", "ct"));
        return formparams;
    }


}
